package com.baoyi.adapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Gallery;
import android.widget.ImageView;

import com.baoyi.content.content;
import com.baoyi.pooke.MainUI;
import com.baoyi.widget.RemoteImageView;

public class LocImagesAdapter extends BaoyiBaseAdapter {
	private Context myContext;
	private List<String> files;

	/* 构造函数 Context */
	public LocImagesAdapter(Context c) {
		files = new ArrayList<String>();
		File fileitem = new File(content.locdir);
		String[] fs = fileitem.list();
		for (int i = 0; i < fs.length; i++) {
			String item = fs[i];
			if (item.endsWith("jpg")) {
				files.add(item);
				if (i > 50) {
					break;
				}
			}
		}
		this.myContext = c;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return files.size();
	}

	@Override
	public Object getItem(int position) {
		String file = files.get(position);
		File fileitem = new File(content.locdir + file);
		FileInputStream inputStream;
		Bitmap image = null;
		try {
			inputStream = new FileInputStream(fileitem);
			image = BitmapFactory.decodeStream(inputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return image;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RemoteImageView imageView = new RemoteImageView(this.myContext);
		String file = files.get(position);
		File fileitem = new File(content.locdir + file);
		FileInputStream inputStream;
		try {
			inputStream = new FileInputStream(fileitem);
			Bitmap image = BitmapFactory.decodeStream(inputStream);
			imageView.setImageBitmap(image);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 填充ImageView
		imageView.setDrawingCacheEnabled(true);
		imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		/* 设置布局参数 */
		imageView.setLayoutParams(new Gallery.LayoutParams(
				MainUI.displayMetrics.widthPixels,
				MainUI.displayMetrics.heightPixels));
		/* 设置背景资源 */
		// imageView.setBackgroundResource(mGalleryItemBackground);
		return imageView;
	}

	@Override
	public String getPicId(int position) {
		// TODO Auto-generated method stub
		return "defalut";
	}

	@Override
	public String getCataLogs(int position) {
		// TODO Auto-generated method stub
		return "defalut";
	}

}
